package alg.kdBaum;
 
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 

import org.w3c.dom.Document;
import org.w3c.dom.Element;
 
public class WriteXMLFile {
	private static Document doc;
	private static Element graph, key;
	private static Element gNode;
	
	private static void addNodes(Knoten node){
		if (node == null || node.getTief() == 10) return;
		
		gNode = doc.createElement("node");
		gNode.setAttribute("id", node.toString());
		graph.appendChild(gNode);




//		<data key="d0">
		Element data = doc.createElement("data");
		data.setAttribute("key", "d0");
		gNode.appendChild(data);
		
		
//      <y:ShapeNode>
		Element yShapeNode = doc.createElement("y:ShapeNode");
		data.appendChild(yShapeNode);
		
//      <y:Fill color="#CCCCFF" transparent="false"/>
		Element fill = doc.createElement("y:Fill");
		fill.setAttribute("transparent", "false");
		fill.setAttribute("color", node.getHexColor());
		yShapeNode.appendChild(fill);
		
//		<y:BorderStyle type="line" width="1.0" color="#000000"/>
		Element borderStyle = doc.createElement("y:BorderStyle");
		borderStyle.setAttribute("type", "line");
		borderStyle.setAttribute("width", "0.5");
		if (node.getTief() % 3 == 0) borderStyle.setAttribute("color", "#FF0000");
		if (node.getTief() % 3 == 1) borderStyle.setAttribute("color", "#00FF00");
		if (node.getTief() % 3 == 2) borderStyle.setAttribute("color", "#0000FF");
		yShapeNode.appendChild(borderStyle);
		
//		<y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="internal" modelPosition="c" textColor="#000000" visible="true" width="102.724609375" x="-36.3623046875" y="5.6494140625">dasdfasdfawecasc</y:NodeLabel>
		Element nodeLabel = doc.createElement("y:NodeLabel");
		nodeLabel.setAttribute("alignment", "center");
		nodeLabel.setAttribute("fontSize", "5");
		nodeLabel.appendChild(doc.createTextNode(node.toString()));
		yShapeNode.appendChild(nodeLabel);

		if (node.left() != null && node.getTief() < 9) {
			Element edge = doc.createElement("edge");
			edge.setAttribute("source", node.toString());
			edge.setAttribute("target", node.left().toString());
			graph.appendChild(edge);
			addNodes(node.left());
		}
		
		if (node.right() != null && node.getTief() < 9) {
			Element edge = doc.createElement("edge");
			edge.setAttribute("source", node.toString());
			edge.setAttribute("target", node.right().toString());
			graph.appendChild(edge);
			addNodes(node.right());
			
		}

	}
	public static void write(Funktionen f) {
		try { 
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
 
		// root elements
		doc = docBuilder.newDocument();
		Element rootElement = doc.createElement("graphml");
		rootElement.setAttribute("xmlns:y", "http://www.yworks.com/xml/graphml");
		rootElement.setAttribute("xmlns", "http://graphml.graphdrawing.org/xmlns");
		rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
		rootElement.setAttribute("xsi:schemaLocation", "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd");
		doc.appendChild(rootElement);
		
//		<key id="d0" for="node" yfiles.type="nodegraphics"/>
		key = doc.createElement("key");
		key.setAttribute("id", "d0");
		key.setAttribute("for", "node");
		key.setAttribute("yfiles.type", "nodegraphics");
				rootElement.appendChild(key);
		
		// staff elements
		graph = doc.createElement("graph");
		graph.setAttribute("edgedefault", "directed");
		graph.setAttribute("id", "G");
		rootElement.appendChild(graph);
 
//		// set attribute to staff element
//		Attr attr = doc.createAttribute("id");
//		attr.setValue("G");
//		graph.setAttributeNode(attr);
 
		// shorten way
		// staff.setAttribute("id", "1");
 
		// firstname elements
		
		addNodes(f.getRoot());
		
//		Element firstname = doc.createElement("firstname");
//		firstname.appendChild(doc.createTextNode("yong"));
//		graph.appendChild(firstname);
// 
//		// lastname elements
//		Element lastname = doc.createElement("lastname");
//		lastname.appendChild(doc.createTextNode("mook kim"));
//		graph.appendChild(lastname);
// 
//		// nickname elements
//		Element nickname = doc.createElement("nickname");
//		nickname.appendChild(doc.createTextNode("mkyong"));
//		graph.appendChild(nickname);
// 
//		// salary elements
//		Element salary = doc.createElement("salary");
//		salary.appendChild(doc.createTextNode("100000"));
//		graph.appendChild(salary);
 
		// write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(new File("file.graphml"));
 
		// Output to console for testing
//		 StreamResult result = new StreamResult(System.out);
 
		transformer.transform(source, result);
 
		System.out.println("File saved!");
 
	  } catch (ParserConfigurationException pce) {
		pce.printStackTrace();
	  } catch (TransformerException tfe) {
		tfe.printStackTrace();
	  }
	}
}